package com.lingnan.controller;

import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.lingnan.pojo.RecruitingPlayers;
import com.lingnan.pojo.SelfActive;
import com.lingnan.pojo.User;
import com.lingnan.service.RecruitingPlayersService;
import com.lingnan.service.SelfActiveService;
import com.lingnan.util.Result;
import com.lingnan.util.ResultGenerator;
import com.lingnan.vo.RecruitingPlayersVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 招募队员表 前端控制器
 * </p>
 *
 * @author 刘宇浩
 * @since 2022-03-13
 */
@RestController
@CrossOrigin
@RequestMapping("/recruitingPlayers")
public class RecruitingPlayersController {

    @Autowired
    private RecruitingPlayersService recruitingPlayersService;

    @Autowired
    private SelfActiveService selfActiveService;

    @GetMapping("/finAll")
    @ApiOperation("查询招募活动总览")
    public Result findAllPlayers(){
        QueryWrapper<RecruitingPlayers> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("end_time");
        List<RecruitingPlayers> list = recruitingPlayersService.list(wrapper);
        if (!ObjectUtils.isEmpty(list)){
            return ResultGenerator.genSuccessResult(200,list,"查询招募活动总览成功！");
        }
        return ResultGenerator.genErrorResult(201,"查询招募活动总览失败");

    }


    @PostMapping("/searchPlayers")
    @ApiOperation("模糊查询招募活动")
    public Result searchPlayers(@ApiParam("模糊查询招募活动,应该传活动名") @RequestBody RecruitingPlayersVo recruitingPlayersVo){
        QueryWrapper<RecruitingPlayers> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("end_time");
        wrapper.like("name", recruitingPlayersVo.getName());
        List<RecruitingPlayers> list = recruitingPlayersService.list(wrapper);
        if (!ObjectUtils.isEmpty(list)){
            return ResultGenerator.genSuccessResult(200,list,"查询招募活动总览成功！");
        }
        return ResultGenerator.genErrorResult(201,"查询招募活动总览失败");

    }
    @PostMapping("/publishPlayers")
    @ApiOperation("招募队员发布")
    public Result publishPlayers(@ApiParam("招募对象")@RequestBody RecruitingPlayersVo recruitingPlayersVo){
        //传学号是将学生发布的招募活动，添加到个人的活动里去
        if (!ObjectUtils.isEmpty(recruitingPlayersVo)){
            //发布招募活动
            recruitingPlayersService.publishPlayers(recruitingPlayersVo);
            return ResultGenerator.genSuccessResult(200,"招募队员发布成功！");
        }
        return ResultGenerator.genErrorResult(201,"招募队员发布失败");
    }

    @PostMapping("/joinActive")
    @ApiOperation("申请加入活动")
    public Result joinActive(@ApiParam("传招募对象和学号")@RequestBody RecruitingPlayersVo recruitingPlayersVo){
        if (ObjectUtils.isEmpty(recruitingPlayersVo)){
            return ResultGenerator.genErrorResult(201,"申请加入活动失败！");
        }
        QueryWrapper<SelfActive> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("active_id",recruitingPlayersVo.getId());
        queryWrapper.eq("no",recruitingPlayersVo.getNo());
        SelfActive one = selfActiveService.getOne(queryWrapper);
        
//        QueryWrapper<RecruitingPlayers> wrapper = new QueryWrapper<>();
//        wrapper.eq("id",recruitingPlayersVo.getId());
//        RecruitingPlayers players = recruitingPlayersService.getOne(wrapper);
        if (ObjectUtils.isEmpty(one)){
            //将前端传过来的招募活动 设置到 我的活动里去
            //添加学号
            SelfActive selfActive = new SelfActive();
            selfActive.setNo(recruitingPlayersVo.getNo());
            selfActive.setName(recruitingPlayersVo.getName());
            selfActive.setPlace(recruitingPlayersVo.getPlace());
            selfActive.setBeginTime(recruitingPlayersVo.getBeginTime());
            selfActive.setEndTime(recruitingPlayersVo.getEndTime());
            selfActive.setContent(recruitingPlayersVo.getContent());
            selfActive.setActiveId(recruitingPlayersVo.getId());

            boolean flag = selfActiveService.save(selfActive);
            if (flag) {
                return ResultGenerator.genSuccessResult(200,"申请加入活动成功！");
            }
            return ResultGenerator.genErrorResult(201,"申请加入活动失败！");
        }
        return ResultGenerator.genErrorResult(202,"已申请过该活动！");

    }

}
